package com.scu.qingshi.start;

import com.scu.qingshi.common.enums.ResponseCodeEnum;
import com.scu.qingshi.common.exception.authorization.AuthorizationException;
import com.scu.qingshi.common.exception.interview.InterviewException;
import com.scu.qingshi.common.response.BaseResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;
import java.text.ParseException;

/**
 * 全局异常拦截器
 * @author qingshi
 */
@RestControllerAdvice(annotations = RestController.class)
public class GlobalExceptionInterceptor {

    @ExceptionHandler(value = {AuthorizationException.class})
    @ResponseBody
    public BaseResponse verifyAuthorityException(AuthorizationException e){
        BaseResponse response = new BaseResponse();
        response.setCode(ResponseCodeEnum.AUTHORITY_ERROR.name());
        response.setMsg("拦截到授权异常:"+e.getDescription());
        response.setSuccess(Boolean.FALSE);
        return response;
    }

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public BaseResponse unknownException(Exception e){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(ResponseCodeEnum.UNKNOWN_ERROR.name());
        baseResponse.setMsg(e.getMessage());
        baseResponse.setSuccess(false);
        return baseResponse;
    }

    @ExceptionHandler(value = {InterviewException.class})
    @ResponseBody
    public BaseResponse interDomainException(InterviewException e){
        BaseResponse response = new BaseResponse();
        response.setCode(ResponseCodeEnum.BIZ_ERROR.name());
        response.setMsg("面试域异常:"+e.getDescription());
        response.setSuccess(Boolean.FALSE);
        return response;
    }

    @ExceptionHandler(value = {ParseException.class})
    @ResponseBody
    public BaseResponse convertDateException(ParseException e){
        BaseResponse response = new BaseResponse();
        response.setCode(ResponseCodeEnum.BIZ_ERROR.name());
        response.setMsg("转换日期异常");
        response.setSuccess(Boolean.FALSE);
        return response;
    }

    @ExceptionHandler(value = {BindException.class})
    @ResponseBody
    public BaseResponse validDataException(BindException e){
        BaseResponse response = new BaseResponse();
        response.setCode(ResponseCodeEnum.ILLEGAL_ARGUMENT.name());
        response.setMsg("参数异常，detail："+e.getMessage());
        response.setSuccess(Boolean.FALSE);
        return response;
    }

}
